package com.example.moshudriver.model;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.BeeFramework.model.BaseModel;
import com.BeeFramework.model.BeeCallback;
import com.example.moshudriver.protocol.ApiInterface;
import com.example.moshudriver.protocol.CONTENT;
import com.example.moshudriver.protocol.LOCATION;
import com.example.moshudriver.protocol.orderpublishRequest;
import com.example.moshudriver.protocol.orderpublishResponse;
import com.example.moshudriver.tools.LocationManager;
import com.example.moshudriver.tools.O2OMobileAppConst;
import com.example.moshudriver.tools.SESSION;
import com.external.androidquery.callback.AjaxStatus;

public class OrderPublishModel extends BaseModel {

	public OrderPublishModel(Context context) {
		super(context);
	}
	
	public void publish(String price, String time, String locationName, String text, int default_receiver_id, int duration) {
		orderpublishRequest request = new orderpublishRequest();
		request.uid = SESSION.getInstance().uid;
		request.sid = SESSION.getInstance().sid;
		request.ver = O2OMobileAppConst.VERSION_CODE;
		request.start_time = time;
		request.offer_price = price;
		request.default_receiver_id = default_receiver_id;
		request.duration = duration;
		LOCATION location = new LOCATION();
		location.lat = LocationManager.getLatitude();
		location.lon = LocationManager.getLongitude();
		location.name = locationName;
		request.location = location;
		CONTENT content = new CONTENT();
		content.text = text;
		request.content = content;
		BeeCallback<JSONObject> cb = new BeeCallback<JSONObject>() {

			@Override
			public void callback(String url, JSONObject jo, AjaxStatus status) {
				try {
					OrderPublishModel.this.callback(this, url, jo, status);
                    if (null != jo) {
                        orderpublishResponse response = new orderpublishResponse();
                        response.fromJson(jo);
                        if(response.succeed == 1) {
                        	
                        	OrderPublishModel.this.OnMessageResponse(url,jo,status);
                        } else {
                        	OrderPublishModel.this.callback(url, response.error_code, response.error_desc);
                        }
                    }
				} catch (JSONException e) {

				}
			}
		};

		Map<String, Object> params = new HashMap<String, Object>();
		try {
			JSONObject publishRequest = request.toJson();
            if(default_receiver_id == 0){
            	publishRequest.remove("default_receiver_id");
            }
        
            if(duration == 0) {
            	publishRequest.remove("duration");
            }
			params.put("json", publishRequest.toString());
		
		} catch (JSONException e) { 
			
		}
		cb.url(ApiInterface.ORDER_PUBLISH).type(JSONObject.class).params(params);
		ajaxProgress(cb);
	}
	
}
